﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace Inspire.Main.UIElements
{
    class NavExpander : Expander, INavHost
    {
        // INavHost NavHostParent
        private INavHost NavHostParent;

        // List of Navigation Buttons
        private List<NavButton> NavigationButtons = new List<NavButton>();

        // StackPanel that contains the buttons of this Expander
        private StackPanel ButtonStacker = new StackPanel();

        /// <summary>
        /// Default constructor for NavExpander
        /// </summary>
        /// <param name="header">Header label for this Expander.</param>
        /// <param name="parent">INavHost parent that navigation requests from NavButton children will propogate to.</param>
        public NavExpander(string header, INavHost parent)
        {
            // Add Margin to the Expander
            this.Margin = new Thickness(5.0, 0.0, 0.0, 0.0);

            // Set Header DataTemplate to Style it
            //this.HeaderTemplate = (DataTemplate)Inspire.App.Current.FindResource("ExpanderHeader");

            // Prepare the StackPanel for buttons
            this.ButtonStacker.Margin = new Thickness(20.0, 5.0, 0.0, 0.0);
            this.ButtonStacker.Orientation = Orientation.Vertical;

            // Add ButtonStacker to this Expander
            this.AddChild(this.ButtonStacker);

            // Prepare this Expander
            this.NavHostParent = parent;
            this.Header = header;
            this.IsExpanded = false;
        }

        /// <summary>
        /// Adds a new NavButton to this Expander
        /// </summary>
        /// <param name="newButton">NavButton to add to this Expander.</param>
        public void AddButton(NavButton newButton)
        {
            // Add button to the LinkedList<NavButton>
            this.NavigationButtons.Add(newButton);

            // Add button to the StackPanel to display
            this.ButtonStacker.Children.Add(newButton);
        }

        /// <summary>
        /// Determine if there is a button in this list for a particular URI
        /// </summary>
        /// <param name="searchUri">The URI to search NavButton children of for.</param>
        /// <returns>true if a NavButton child is found that directs the user to the searchUri; false, otherwise.</returns>
        public bool HasUri(string searchUri)
        {
            foreach (NavButton nextButton in this.NavigationButtons)
            {
                if (nextButton.NavPageUri.Equals(searchUri))
                {
                    return true;
                }
            }
            return false;
        }
        
        /// <summary>
        /// Override method for implementing INavHost.
        /// </summary>
        /// <param name="navigationUri">URI to propogate to the parent of this expander for navigation.</param>
        public void PropogateNavUp(string navigationUri)
        {
            // Determine if this Expander has a button matching navigationUri
            // If it does, disable the button. All other buttons are enabled.
            foreach (NavButton nextButton in this.NavigationButtons)
            {
                if (nextButton.NavPageUri.Equals(navigationUri))
                {
                    nextButton.IsEnabled = false;
                }
                else
                {
                    nextButton.IsEnabled = true;
                }
            }

            this.NavHostParent.PropogateNavUp(navigationUri);
        }

        /// <summary>
        /// Override method for implementing INavHost.
        /// </summary>
        /// <param name="navigationUri"></param>
        public void PropogateNavDown(string navigationUri)
        {
            // Determine if this Expander has a button matching navigationUri.
            // If it does, disable the button. All other buttons are enabled.
            foreach (NavButton nextButton in this.NavigationButtons)
            {
                if (nextButton.NavPageUri.Equals(navigationUri))
                {
                    nextButton.IsEnabled = false;
                }
                else
                {
                    nextButton.IsEnabled = true;
                }
            }
        }
    }
}
